﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using XDAFacebook;
using XFControls.Utils;

namespace FBEntities.FBObjects
{
    public class FBPlaces
    {
        public List<FBPlace> data;
        public string Search { get; set; }
        public int Distance = 1000;

        private string _lat;
        private string _lon;

        public FBPlaces(string lat, string lon)
        {
            _lat = lat;
            _lon = lon;

            data = new List<FBPlace>();
        }

        public void Parse(string json)
        {
            if (string.IsNullOrEmpty(json))
                return;

            JObject o = JObject.Parse(json);
            FBParseUtilities.Parse(o, this);
        }

        public WebDownloaderWrapper GetDownloadWrapper()
        {
            var url = "https://graph." + Settings.Domain + ".com/search";
            string authKeyCode = FBParseUtilities.authKeySplit;

            var args = new Dictionary<string, string>();
            args.Add("type", "place");

            if (!string.IsNullOrEmpty(Search))
                args.Add("search", Search);

            string lat = _lat;
            string lon = _lon;

            lat = lat.Replace(',', '.');
            lon = lon.Replace(',', '.');

            args.Add("center", lat + "," + lon);
            args.Add("distance", Distance.ToString());
            args.Add("access_token", authKeyCode);

            return new WebDownloaderWrapper(url) { PostData = args };
        }
    }


    public class FBPlace : IFBObject
    {
        public string id;
        public string name;
        public FBLocation location;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

    public class FBLocation : IFBObject
    {
        public string street;
        public string city;
        public string state;
        public string country;
        public string zip;
        public double latitude;
        public double longitude;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

    public class FBApplication : IFBObject
    {
        public string id;
        public string name;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

}
